<?php

define('IN_PROJECTOR', true);//用于防止模块文件被直接运行
define('S_ROOT', dirname(__FILE__).'/');//网站根目录常量
error_reporting(E_ERROR | E_WARNING | E_PARSE);//Debug
set_magic_quotes_runtime(0);//魔法括号

function __autoload($classname){
	require_once './model/'.$classname.'.class.php';
}

function writeconfig($config, $value){
	file_put_contents('./data/'.$config.'.inc.php', '<?php return '.var_export($value, true).';?>');
}

require_once './core/global.func.php';

$_G = array();
@$_G['setting'] = array_merge(@include './data/config.inc.php', @include './data/stconfig.inc.php');
@$_G['setting']['db'] = include './data/dbconfig.inc.php';
$_G['style'] = 'default';

if(!file_exists(S_ROOT.'./data/install.lock')){
	
	if($_POST){
		$config = array(
			'home_unusedratio' => 10,
			'home_deadlinelimit' => 14,
			'timezone' => intval($_POST['config']['timezone']),
			'timefix' => 0,
			'cookiepre' => randomstr(3).'_',
			'charset' => 'utf-8',
			'style' => 'fundation',
		);
		writeconfig('config', $config);
		
		$stconfig = array(
			'salt' => randomstr(32),
		);
		writeconfig('stconfig', $stconfig);
		
		$db = array(
			'type' => $_POST['db']['type'],
			'charset' => 'utf8',
			'server' => $_POST['db']['server'],
			'user' => $_POST['db']['user'],
			'tpre' => $_POST['db']['tpre'],
			'pw' => $_POST['db']['pw'],
			'name' => $_POST['db']['name'],
			'pconnect' => intval($_POST['db']['pconnect']),
		);
		writeconfig('dbconfig', $db);
		
		$dbo = new Mysql();
		$dbo->connect($db['server'], $db['user'], $db['pw'], '', $db['pconnect']);
		$dbo->set_tablepre($db['tpre']);
		
		$dbo->query("DROP DATABASE IF EXISTS `$db[name]`");
		$dbo->query("CREATE DATABASE `$db[name]`");
		
		$dbo->select_db($db['name']);

		$sql = array(
			'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"',
			'SET time_zone = "+00:00";',
			'DROP TABLE IF EXISTS `'.$db['tpre'].'administrator`;',
			'CREATE TABLE IF NOT EXISTS `'.$db['tpre'].'administrator` ('
			  .'`uid` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,'
			  .'`username` varchar(15) NOT NULL,'
			  .'`email` varchar(50) NOT NULL,'
			  .'`pwmd5` char(32) NOT NULL,'
			  .'`permission` int(8) NOT NULL,'
			  .'`avatar` varchar(255) NOT NULL,'
			  .'`logintime` int(10) unsigned NOT NULL,'
			  .'`loginip` char(15) NOT NULL,'
			  .'`lastlogin` int(10) unsigned NOT NULL,'
			  .'PRIMARY KEY (`uid`)'
			.') ENGINE=MyISAM  DEFAULT CHARSET='.$db['charset'].' AUTO_INCREMENT=1 ;',
			
			'DROP TABLE IF EXISTS `'.$db['tpre'].'project`;',
			'CREATE TABLE IF NOT EXISTS `'.$db['tpre'].'project` ('
			  .'`id` mediumint(8) NOT NULL AUTO_INCREMENT,'
			  .'`code` int(30) NOT NULL,'
			  .'`name` varchar(200) NOT NULL,'
			  .'`type` tinyint(3) NOT NULL,'
			  .'`source` varchar(100) NOT NULL,'
			  .'`sourceaccount` varchar(100) NOT NULL,'
			  .'`sponsortype` varchar(32) NOT NULL,'
			  .'`amount` decimal(11,2) NOT NULL,'
			  .'`restamount` decimal(11,2) NOT NULL,'
			  .'`creditedtime` int(10) unsigned NOT NULL,'
			  .'`dateline` int(10) unsigned NOT NULL,'
			  .'`target` VARCHAR( 100 ) NOT NULL,'
			  .'`specifieduses` varchar(200) NOT NULL,'
			  .'`thankletter` varchar(255) NOT NULL,'
			  .'`contrast` varchar(255) NOT NULL,'
			  .'`responsor` varchar(100) NOT NULL,'
			  .'`responsormobile` int(11) unsigned NOT NULL,'
			  .'`deadline` int(10) unsigned NOT NULL,'
			  .'`status` tinyint(1) NOT NULL,'
			  .'PRIMARY KEY (`id`),'
			  .'KEY `code` (`code`)'
			.') ENGINE=MyISAM  DEFAULT CHARSET='.$db['charset'].' AUTO_INCREMENT=1 ;',

			'DROP TABLE IF EXISTS `'.$db['tpre'].'projectprocess`;',
			'CREATE TABLE IF NOT EXISTS `'.$db['tpre'].'projectprocess` ('
			  .' `id` int(10) unsigned NOT NULL AUTO_INCREMENT,'
			  .' `pid` mediumint(8) NOT NULL,'
			  .' `amount` decimal(11,2) NOT NULL,'
			  .' `reason` varchar(255) NOT NULL,'
			  .' `dateline` int(10) unsigned NOT NULL,'
			  .' PRIMARY KEY (`id`),'
			  .' KEY `pid` (`pid`),'
			  .'`type` TINYINT( 1 ) NOT NULL ,'
			  .'`consumer` VARCHAR( 100 ) NOT NULL ,'
			  .'`responsor` VARCHAR( 100 ) NOT NULL '
			.') ENGINE=MyISAM  DEFAULT CHARSET='.$db['charset'].' AUTO_INCREMENT=1 ;',

			'DROP TABLE IF EXISTS `'.$db['tpre'].'projecttype`;',
			'CREATE TABLE IF NOT EXISTS `'.$db['tpre'].'projecttype` ('
			  .' `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,'
			  .' `name` varchar(100) NOT NULL,'
			  .' PRIMARY KEY (`id`)'
			.') ENGINE=MyISAM  DEFAULT CHARSET='.$db['charset'].' AUTO_INCREMENT=1 ;',
		);

		foreach($sql as $l){
			$dbo->query($l);
		}
		
		$tpre = $db['tpre'];
		unset($db);
		$db = &$dbo;

		$admin = new Admin();
		$admin->v['username'] = $_POST['admin']['username'];
		$admin->v['password'] = $_POST['admin']['password'];
		$admin->v['permission'] = 0x1;
		$admin->register();

		touch(S_ROOT.'./data/install.lock');
		
		showmsg('安装成功！', 'index.php');
	}else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>四川大学教育基金会项目管理系统<?php echo $navtitle;?></title>
<link href="./image/default/common.css" type="text/css" rel="stylesheet" />
<link href="./image/easyui.css" type="text/css" rel="stylesheet" />
<script src="./js/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="./js/jquery.easyui.min.js" type="text/javascript"></script>
<script src="./js/common.js" type="text/javascript"></script>
</head>

<body<?php if(defined('CURSCRIPT')) echo ' class="'.CURSCRIPT.'"';?>">

<div class="container">

	<div class="nav">
		<div class="left"></div>
		<ul id="navlist" class="middle" style="width:960px;">
		</ul>
		<div class="right"></div>
		<script type="text/javascript">
		var index = true;
		$('#navlist a').each(function(){
			if(this.href == location.href){
				$(this).addClass('current');
				index = false;
			}
		});
		</script>
	</div>
	
	<div class="content">
		<form action="install.php" method="post">
		<h1>数据库配置</h1>
		<table>
			<tr><th><label>数据库类型：</label></th><td><input type="text" id="db[type]" name="db[type]" value="<?php echo $_G['setting']['db']['type']?>" /></td></tr>
			<tr><th><label>数据库字符集：</label></th><td><input type="text" id="db[charset]" name="db[charset]" value="<?php echo $_G['setting']['db']['charset']?>" /></td></tr>
			<tr><th><label>数据库服务器地址：</label></th><td><input type="text" id="db[server]" name="db[server]" value="<?php echo $_G['setting']['db']['server']?>" /></td></tr>
			<tr><th><label>数据库账号：</label></th><td><input type="text" id="db[user]" name="db[user]" value="<?php echo $_G['setting']['db']['user']?>" /></td></tr>
			<tr><th><label>数据库密码：</label></th><td><input type="text" id="db[pw]" name="db[pw]" value="<?php echo $_G['setting']['db']['pw']?>" /></td></tr>
			<tr><th><label>数据库表前缀：</label></th><td><input type="text" id="db[tpre]" name="db[tpre]" value="<?php echo $_G['setting']['db']['tpre']?>" /></td></tr>
			<tr><th><label>数据库名：</label></th><td><input type="text" id="db[name]" name="db[name]" value="<?php echo $_G['setting']['db']['name']?>" /></td></tr>
			<tr><th><label>是否持续链接：</label></th><td><input type="radio" id="db[pconnect]" name="db[pconnect]" value="0" checked="checked" />否 <input type="radio" id="db[pconnect]" name="db[pconnect]" value="0" />是</td></tr>
		</table>
		
		<h1>系统设置</h1>
		<table>
			<tr><th><label>时区设置：</label></th><td><input type="text" id="config[timezone]" name="config[timezone]" value="<?php echo $_G['setting']['timezone']?>" /></td></tr>
			<tr><th><label>初始管理员账号：</label></th><td><input type="text" id="admin[username]" name="admin[username]" /></td></tr>
			<tr><th><label>初始管理员密码：</label></th><td><input type="text" id="admin[password]" name="admin[password]" /></td></tr>
		</table>
		
		<button type="submit">安装SCU-Projector</button>
		
		</form>
	</div>

	<div class="footer">
		<div class="mark">
			<img src="./image/logo_white.png" />
		</div>
		<div class="split"></div>
		<div class="copyright">
			<p>Powered By <a href="http://cs.scu.edu.cn" target="_blank">Computer Science College</a> of <a href="http://www.scu.edu.cn" target="_blank">Sichuan University</a></p>
		</div>
	</div>

</div>

</body>
</html>
<?php	
	}

}else{
	showmsg('已经安装过Projector，要重新安装请删除./data/install.lock文件。');
}

?>